package springboot.file.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springboot.file.demo.common.Result;
import springboot.file.demo.pojo.vo.DemoExcelVo;
import springboot.file.demo.pojo.vo.ExcelDto;
import springboot.file.demo.pojo.vo.ExcelVO;
import springboot.file.demo.service.ExportExcelService;
import springboot.file.demo.util.ExportExcelUtils;
import springboot.file.demo.util.ExportTemplateUtil;
import springboot.file.demo.util.UploadUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/20 14:49
 * @Version: 1.0
 */
@RestController
@RequestMapping("/file")
@Api(tags = "测试接口")
public class FileController {

    @Autowired
    UploadUtils uploadUtils;
    @Autowired
    ExportExcelService exportExcelService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result uploadFile(@RequestPart(value = "file")MultipartFile file){

        return uploadUtils.upload(file);

    }

    /**
     * 上传文件
     */
    @GetMapping("/downLoad")
    public Result downLoad(@RequestParam(value = "fileName")String fileName, HttpServletResponse response){

//        return uploadUtils.getDownloadUrl(fileName, "");

        try {
            uploadUtils.downLoad(fileName,response);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("失败");
        }

    }

    /**
     * 导出信息
     * @return Excel流
     */
    @RequestMapping("/exportExcel")
//    @Log(function = "数据导出", operationType = OperationType.EXPORT)
    public void exportExcel(/*@RequestBody Map<String, String> param,*/ HttpServletResponse response) {
        try{
            System.out.println(",,,,,,,,,,导出请求");
            String fileName = "导出信息维护.xlsx";
            //查询获取导出的数据
            List<DemoExcelVo> demoList = exportExcelService.exportData();
            Map<String, Object> map = new HashMap<>(16);
            map.put("userList", demoList);
            ExcelVO excelVO = new ExcelVO();
            excelVO.setFileName(fileName);
            excelVO.setMap(map);
            excelVO.setTemplateUrl("excel/defectImageTemplate.xlsx");
//            excelVO.setTemplateNewUrl("excel/defectImage.xlsx");
            System.out.println("+++++++++++++++++导出："+demoList.size()+"条");
            new ExportTemplateUtil().exportTemplate(excelVO, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 导出信息
     * @return Excel流
     */
    @PostMapping("/exportExcel2")
    @ApiOperation("导出")
    public void exportExcel2(@RequestBody List<ExcelDto> list, HttpServletResponse response) {

        try{
            System.out.println(",,,,,,,,,,导出请求");
            String fileName = "导出信息维护.xlsx";
            //查询获取导出的数据
//            List<DemoExcelVo> demoList = exportExcelService.exportData();
            Map<String, Object> map = new HashMap<>(16);
            map.put("userList", list);
            ExcelVO excelVO = new ExcelVO();
            excelVO.setFileName(fileName);
            excelVO.setMap(map);
            excelVO.setTemplateUrl("excel/demo.xlsx");
//            excelVO.setTemplateNewUrl("excel/defectImage.xlsx");
            System.out.println("+++++++++++++++++导出："+list.size()+"条");
            new ExportTemplateUtil().exportTemplate(excelVO, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Autowired
    ExportExcelUtils exportExcelUtils;

    //下载成员信息模板文件
    @PostMapping("/downloadTemplate")
    @ApiOperation(value="下载数据表导入模板", notes="详细描述")
    public void downloadTemplate(HttpServletResponse response,@RequestBody List<ExcelDto> list) throws IOException, IllegalAccessException {
        //获得输出流
        ServletOutputStream out = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=temp.xls");
        response.setContentType("application/msexcel");
        // 输出 excel 表格数据
        exportExcelUtils.createExcel(list,out);
    }

}
